package com.ruoyi.psi.purchase.purchasereturn.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.psi.purchase.purchasereturn.domain.PurchaseReturn;
import com.ruoyi.psi.purchase.purchasereturn.domain.PurchaseReturnList;
import com.ruoyi.psi.purchase.purchasereturn.service.IPurchaseReturnService;
import com.ruoyi.psi.purchase.storage.domain.PurchaseStorage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 采购订单退货
 */
@RestController
@RequestMapping("/psi/purchase/return")
@Api(value = "采购订单退货模块", tags = "采购订单退货接口")
public class PurchaseReturnController extends BaseController {


    @Autowired
    private IPurchaseReturnService purchaseReturnService;

    /**
     * 获取采购订单退货
     * @param purchaseReturn
     * @return
     */
    @ApiOperationSupport(includeParameters = {"PurchaseReturn.purchaseReturnId", "PurchaseReturn.warehouseId", "PurchaseReturn.supplierInfoId", "PurchaseReturn.returnTime"})
    @ApiOperation(value = "获取采购订单退货列表",notes = "查询所有采购订单退货信息")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody PurchaseReturn purchaseReturn) {
        startPage();

        List<PurchaseReturn> purchaseReturns = purchaseReturnService.selectPurchaseReturnList(purchaseReturn);
        return getDataTable(purchaseReturns);
    }

    /**
     * 导出采购订单退货
     *
     * @param purchaseReturn
     * @return
     */
    @PostMapping("/export")
    public AjaxResult export(@RequestBody PurchaseReturn purchaseReturn) {
        List<PurchaseReturn> purchaseReturns = purchaseReturnService.selectPurchaseReturnList(purchaseReturn);
        ExcelUtil<PurchaseReturn> util = new ExcelUtil<PurchaseReturn>(PurchaseReturn.class);
        return util.exportExcel(purchaseReturns, "采购订单退货单数据");
    }

    /**
     * 新增时获取入库单详细
     * @param purchaseStorage
     * @return
     */
    @PostMapping("/getPurchaseStorageForReturn")
    public AjaxResult getPurchaseStorageForReturn(@RequestBody PurchaseStorage purchaseStorage) {
        List<PurchaseReturnList> purchaseReturnLists = purchaseReturnService.getPurchaseStorageForReturn(purchaseStorage.getPurchaseStorageId());
        return AjaxResult.success(purchaseReturnLists);
    }

    /**
     * 新增采购订单退货
     * @param purchaseReturn 采购订单退货单据
     * @return
     */
    @ApiOperation(value = "新增采购订单退货", notes = "新增采购订单退货信息")
    @ApiImplicitParam(name = "purchaseReturn", value = "新增采购订单退货(参数：purchaseReturnId，purchaseStorageId，warehouseId，returnTime，remark)", dataType = "PurchaseReturn")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody PurchaseReturn purchaseReturn) {
        try {
            purchaseReturnService.insertPurchaseReturn(purchaseReturn);
            return AjaxResult.success("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增失败");
        }
    }

    /**
     *
     * 更新采购订单退货
     * @param purchaseReturn 采购订单退货单据
     * @return
     */
    @ApiOperation(value = "更新采购订单退货", notes = "更新采购订单退货")
    @ApiImplicitParam(name = "purchaseReturn", value = "更新采购订单退货", dataType = "PurchaseReturn")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody PurchaseReturn purchaseReturn) {
        try {
            purchaseReturnService.updatePurchaseReturn(purchaseReturn);
            return AjaxResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除采购订单退货
     * 仅修改删除标志位
     *
     * @param purchaseReturn 采购订单退货单据
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody PurchaseReturn purchaseReturn) {

        return toAjax(purchaseReturnService.updatePurchaseReturnDeleteFlag(purchaseReturn));
    }

}
